package com.codedef.library.controller;

import com.codedef.library.common.response.Result;
import com.codedef.library.common.valid.AddGroup;
import com.codedef.library.common.valid.UpdateGroup;
import com.codedef.library.entity.Task;
import com.codedef.library.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/task")
public class TaskController {

    @Autowired
    TaskService taskService;

    @PostMapping("/add")
    public Result addOneTask(@Validated({AddGroup.class}) @RequestBody Task task) {
        log.info("接收到的Task" + task);
        return taskService.addOne(task) ? Result.SUCCESS() : Result.FAIL();
    }

    /**
     * 查询数据库中所有的任务记录
     * @return
     */
    @GetMapping("/list")
    public Result taskList() {
        return Result.SUCCESS(taskService.getAllTask());
    }

    /**
     * 更新一个Task
     * @param task
     * @return
     */
    @PostMapping("/update")
    public Result updateOne(@Validated({UpdateGroup.class}) @RequestBody Task task) {
        return taskService.updateOneTask(task) ? Result.SUCCESS() : Result.FAIL();
    }

    @GetMapping("/exec")
    public Result execTask() {
//        new Thread(() -> ).start();
        taskService.timingevice();
        return Result.SUCCESS("开始手动执行");
    }

    @GetMapping("/all")
    public Result getAllTask() {
        return Result.SUCCESS(taskService.queryAll());
    }

}
